package com.twu.biblioteca.model.session;

import com.twu.biblioteca.model.session.exceptions.InvalidSessionException;
import com.twu.biblioteca.model.users.SecurityUtils;
import com.twu.biblioteca.model.users.Username;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * User: cschlumb
 * Date: 09/08/2013
 * Time: 15:04
 * To change this template use File | Settings | File Templates.
 */
public class SessionManager {

    HashMap<Session, Username> sessionList = new HashMap<Session, Username>();

    public Session generateSession() {
        return new Session(SecurityUtils.getRandomSalt());
    }

    public void linkSession(Session session, Username username){
        sessionList.put(session, username);
    }

    public void invalidate(Session session){
        sessionList.remove(session);
    }

    public Username validate(Session session) throws InvalidSessionException {
        if (!isValid(session))
            throw new InvalidSessionException();

        return sessionList.get(session);
    }

    public boolean isValid(Session session){
        if (sessionList.containsKey(session))
            return true;
        return false;
    }
}
